//
//  FriendsListVC.m
//  SelfDriving
//
//  Created by apple on 16/1/11.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "FriendsListVC.h"
#import "BlackCell.h"
#import "BusinessService+User.h"
#import "FriendDetailsMainVC.h"
@interface FriendsListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

@implementation FriendsListVC{
    NSMutableArray *data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    data = [[NSMutableArray alloc]init];
    [self initdata];
}
-(void)initdata{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    NSLog(@"走到这了1");
    //xhw2018-2-6改bug15606
    //如果是用其他手机绑定的，就不会取到openid，就是nil，所以崩溃，注释掉下面传openid
//    if ([_type isEqualToString:@"3"]) {
//
//        [dic setObject:[UserDefaults readUserDefaultObjectValueForKey:@"sinaUsid"] forKey:@"openids"];
//    }else{
//        [dic setObject:[UserDefaults readUserDefaultObjectValueForKey:@"tencentUsid"] forKey:@"openids"];
//    }
    [dic setObject:_type forKey:@"type"];
    NSLog(@"走到这了2");
    [[BusinessService shareInstance]thirdfriend:dic success:^(id result) {
        data = result;
        [_table reloadData];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return data.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LoginResponseModel *user = [data objectAtIndex:indexPath.row];
    BlackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FriendsCell"];
    cell.imghead.layer.cornerRadius = cell.imghead.frame.size.width/2;
    cell.imghead.image = [UIImage imageNamed:@"notimgs"];
    cell.labelname.text = user.nickname;
    [ValidateHelper setImage:user.headsmall img:cell.imghead istoux:YES];
    cell.btnadd.layer.cornerRadius = 5;
    cell.content.text = user.sign;
    cell.btnadd.clipsToBounds = YES;
    cell.btnadd.model = user;
    if ([user.isfriend intValue]==1) {//已添加好友
        cell.btnadd.titleLabel.text = @"已关注";
        [cell.btnadd setTitle:@"已关注" forState:UIControlStateNormal];
        cell.btnadd.enabled = NO;
    }else if ([user.isfriend intValue]==2){
        cell.btnadd.titleLabel.text = @"粉丝";
        [cell.btnadd setTitle:@"粉丝" forState:UIControlStateNormal];
        cell.btnadd.enabled = NO;
    }else if ([user.isfriend intValue]==3){
        cell.btnadd.titleLabel.text = @"好友";
        [cell.btnadd setTitle:@"好友" forState:UIControlStateNormal];
        cell.btnadd.enabled = NO;
    }
    return  cell;
}
- (IBAction)addHyAction:(BaseButton *)sender {
    LoginResponseModel *model = sender.model;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"0" forKey:@"type"];
    [dic setObject:model.uid forKey:@"fid"];
    [[BusinessService shareInstance]follow:dic success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"添加成功"];
        [self initdata];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LoginResponseModel *user = [data objectAtIndex:indexPath.row];
    DTModel *model = [[DTModel alloc]init];
    model.uid = user.uid;
    
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = model;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}

- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
